package group.rober.jeki.controller;

import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice
public class JekiErrorControllerAdvice {

    @ExceptionHandler(value = {UnknownAccountException.class})
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ModelAndView logonError(HttpServletRequest request, HttpServletResponse response, Exception e) throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("e", e);
        mv.setViewName("views/exception");
        response.setStatus(401);
        return mv;
    }
}
